'''
Created on 25.Mar.2009

@author: okaya
'''
import elementtree.ElementTree as ET
from xml.etree.ElementTree import ElementTree,Element,SubElement,parse

def indent(elem, level=0):
    i = "\n" + level*"  "
    if len(elem):
        if not elem.text or not elem.text.strip():
            elem.text = i + "  "
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
        for elem in elem:
            indent(elem, level+1)
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
    else:
        if level and (not elem.tail or not elem.tail.strip()):
            elem.tail = i

root = ET.Element('html')

head = ET.SubElement(root,'head')
script = ET.SubElement(head,'script')
script.set('type','text/javascript')
script.text = "var a = 'I love &aacute; letters'"

body = ET.SubElement(root,'body')
h1 = ET.SubElement(body,'h1')
h1.text = "And I like the fact that 3 > 1"

panel = ET.SubElement(root,'panel')
panel.set('panel_id','main')

bgcolor=ET.SubElement(panel,'bgcolor')
bgcolor.text='#FFFFFF'
transparent=ET.SubElement(panel,'transparent')
transparent.text='1'

tree = ET.ElementTree(root)

indent(root)
tree.write('out.xml')

